#!/bin/bash
#
# https://github.com/Aniverse/inexistence
# Author: Aniverse
################################################################################################
script_update=2020.03.29
script_version=r11002
outputpath="/log/torrents"
################################################################################################
black=$(tput setaf 0); red=$(tput setaf 1); green=$(tput setaf 2); yellow=$(tput setaf 3); blue=$(tput setaf 4)
magenta=$(tput setaf 5); cyan=$(tput setaf 6); white=$(tput setaf 7); bold=$(tput bold); normal=$(tput sgr0); jiacu=${normal}${bold}

[[ -z $1 ]] && { echo -e "\n${red}${bold}WARNING${jiacu} You must input a path to your file${normal}\n" ; exit 1 ; }
piece_size=$2
[[ -z $piece_size ]] && piece_size=24
#[[ $3 == -d ]] && DeBUG=1
[[ ! $(which mktorrent) ]] && { echo -e "No mktorrent found in PATH!" ; exit  1; }
################################################################################################

mkdir -p $outputpath

filepath=`echo "$1"`
file_title=$(basename "$filepath")
file_title_clean="$(echo "$file_title" | tr '[:space:]' '.')"
file_title_clean="$(echo "$file_title_clean" | sed s'/[.]$//')"
file_title_clean="$(echo "$file_title_clean" | tr -d '(')"
file_title_clean="$(echo "$file_title_clean" | tr -d ')')"
file_title_clean="`   echo "$file_title_clean" | sed 's/\//\./'   `"

# Ask for Tracker
echo -e "
01) ${cyan}Create a new torrent with empty announce${normal}
02) ${cyan}Create a new torrent and specify an announce${normal}
11) ${cyan}Create a new torrent for HD-Torrents${normal}
12) ${cyan}Create a new torrent for Classix-Unlimited${normal}
99) ${cyan}Create a new torrent for public trackers${normal}"
echo -ne "${yellow}${bold}Which tracker would you like to use?${normal} (Default: ${cyan}01${normal}) "; read -e responce

case $responce in
    01 | 1 ) ANNOUNCE="-a \"\""
             echo -e "The script will create a new torrent with empty announce" ;;
    02 | 2 ) echo "${yellow}${bold}" ; read -e -p "Input your tracker announce: ${normal}${blue}" TRACKERA ; echo "${normal}" ; ANNOUNCE="-a $TRACKERA"
             echo -e "The script will create a new torrent with the announce you input" ;;
    11 ) ANNOUNCE="-a http://hdts-announce.ru/announce.php" 
         echo -e "The script will create a new torrent with HD-Torrents' announce" ;;
    12 ) ANNOUNCE="-a http://classix-unlimited.co.uk/announce.php"
         echo -e "The script will create a new torrent with Classix-Unlimited' announce" ;;
    99 ) ANNOUNCE="-a udp://tracker.coppersurfer.tk:6969/announce -a http://open.kickasstracker.com:80/announce -a http://bt.dl1234.com:80/announce  -a udp://tracker.safe.moe:6969/announce -a udp://9.rarbg.to:2710/announce -a udp://tracker.piratepublic.com:1337/announce -a http://tracker.opentrackr.org:1337/announce -a http://retracker.telecom.by:80/announce -a https://open.acgnxtracker.com:443/announce -a udp://tracker.xku.tv:6969/announce -a udp://thetracker.org:80/announce -a udp://bt.xxx-tracker.com:2710/announce -a http://0d.kebhana.mx:443/announce -a http://share.camoe.cn:8080/announce -a udp://inferno.demonoid.pw:3418/announce -a udp://tracker.cypherpunks.ru:6969/announce"
         echo -e "The script will create a new torrent with public trackers' announce" ;;
    "" | * ) newtorrent=Yes; ANNOUNCE="-a \"\"" ;;
esac

echo
starttime=$(date +%s)

#[[ $DeBUG == 1 ]] && echo "ANNOUNCE=$ANNOUNCE"
mktorrent -v -p -l $piece_size $ANNOUNCE -o "${outputpath}/$file_title_clean.torrent" "$filepath"
if [ ! $? -eq 0 ];then exit 1; else

endtime=$(date +%s) 
timeused=$(( $endtime - $starttime ))

clear
echo -e "${bold}Done. Created torrent is stored in ${yellow}\"${outputpath}\"${normal}"
if [[ $timeused -gt 60 && $timeused -lt 3600 ]]; then
    timeusedmin=$(expr $timeused / 60)
    timeusedsec=$(expr $timeused % 60)
    echo -e "${bold}Time used ${timeusedmin} min ${timeusedsec} sec${normal}"
elif [[ $timeused -ge 3600 ]]; then
    timeusedhour=$(expr $timeused / 3600)
    timeusedmin=$(expr $(expr $timeused % 3600) / 60)
    timeusedsec=$(expr $timeused % 60)
    echo -e "${bold}Time used ${timeusedhour} hour ${timeusedmin} min ${timeusedsec} sec${normal}"
else
   echo -e "${bold}Time used ${timeused} sec${normal}"
fi

echo

fi


debug_used() {

echo -n > $(which zuozhong) ; nano $(which zuozhong)
rm -f *torrent
touch 1
zuozhong 1

}
